package applet;

import processing.core.PApplet;

	public class WeeklyButton extends Button {

		public boolean pressed = false;
		public boolean dragged = false;
		private int prevPaintX, prevPaintY;
		private int dx, dy;
		
		public WeeklyData weeklyData;
		
		

		@Override
		public void draw()
		{
			int cx = x;
			int cy = y;
			
			

			if(mouseOver()) //draw the menu of sub-categories
			{
				if(!active)
					if(GLOBAL.processing.mousePressed)
					{
						//GLOBAL.processing.fill(GLOBAL.colorButtonActive);
						GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(150, 150, 200), 255);
						GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(150, 150, 200), 150);
						cx += 1;
						cy += 1;
					}
					else
					{
						GLOBAL.processing.fill(GLOBAL.colorButtonBackground);
						GLOBAL.processing.rectMode(PApplet.CORNER);
						GLOBAL.processing.rect(x, y, width, height);
						GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(170, 150, 200), 255);
						GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(170, 150, 200), 150);
					}
				else
				{
					if(GLOBAL.processing.mousePressed)
					{
						cx+=1;
						cy+=1;
						GLOBAL.processing.fill(GLOBAL.colorButtonActive,0);
					}
					
					else
					{
						GLOBAL.processing.fill(GLOBAL.colorButtonActive,255);
						GLOBAL.processing.rectMode(PApplet.CORNER);
						GLOBAL.processing.rect(x, y, width, height);
					}
					GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(255, 0, 0), 255);
					GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(255, 0, 0), 150);
				}
					
			}
			else
			{
				if(active)
				{
					GLOBAL.processing.noStroke();
					GLOBAL.processing.fill(GLOBAL.colorButtonActive);
					GLOBAL.processing.rect(x, y, width, height);
					GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(150, 150, 200), 255); //frame of the button
					GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(150, 150, 200), 150);
				}
				else
				{
					GLOBAL.processing.rectMode(PApplet.CORNER);

			    	GLOBAL.processing.noStroke();
			    	GLOBAL.processing.fill(GLOBAL.colorButtonBackground/*backColor*/);
			    	GLOBAL.processing.rect(x, y, width, height);
			    	
			    	GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(255, 255, 255), 255);
			    	GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(0, 0, 0), 180);
				}
			}
				
			if(label != null)
			{
				//GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);
				GLOBAL.processing.strokeWeight(2);
				GLOBAL.processing.fill(GLOBAL.colorButtonLabel);
				GLOBAL.processing.textAlign(PApplet.CENTER, PApplet.CENTER);
				GLOBAL.processing.textFont(GLOBAL.tFont,fontSize);
				if(icon!=null)
					GLOBAL.processing.text(label, cx + (width/2) + 10, cy + (height/2));
				else
				{
					if(height == 38) //ageButtons
					{
						GLOBAL.processing.textFont(GLOBAL.tFont32);
						GLOBAL.processing.text(label, cx + (width/2), cy + (height/2));
					}
					else
					{
						GLOBAL.processing.textFont(GLOBAL.tFont,fontSize);
						GLOBAL.processing.text(label, cx + (width/2), cy + (height/2));
					}
				}
					
			}

			if(icon != null)
			{
				GLOBAL.processing.imageMode(PApplet.CENTER);
				if(width == 18)
					GLOBAL.processing.image(icon, x+width/2, y+height/2, 14+(cx-x),14+(cy-y));
				else if(width == 190) //sexButton
					GLOBAL.processing.image(icon, x+width/2, y+height/2, 0.8f*(width-(cx-x)-5),0.8f*(width-(cy-y)-5));
				else
					GLOBAL.processing.image(icon, x+5, y+5, 16,16);
				
			}
			
			if(dragged == true)
				mouseDragged();
		}
		
		public void mouseReleased()
		{
			if (mouseOver() || dragged == true /*&& dragged into specific view area*/)
			{
				active = !active;
			}
//			if(dragged == true && GLOBAL.onDynTree == true)
//				GLOBAL.draggedToDynTree = true;
			
			GLOBAL.weeklyDataCurrent = weeklyData;
			GLOBAL.weeklyImage = GLOBAL.processing.loadImage(GLOBAL.weeklyDataCurrent.image_large);

			dragged = false;
			pressed = false;
			//if(GLOBAL.artistsCompare) {
				active = false;
			//}
		}
		
		public void mousePressed()
		{
			if(mouseOver())
			{
				pressed = true;
				dx = GLOBAL.processing.mouseX - x;
				dy = GLOBAL.processing.mouseY - y;
			}
		}
		
		public void mouseDragged()
		{
			if(prevPaintX!=0 || prevPaintY != 0)
			{
				//erase the previous shadow
				GLOBAL.processing.noStroke();
				GLOBAL.processing.fill(GLOBAL.colorBackground,0);
				GLOBAL.processing.rect(prevPaintX, prevPaintY, width, height);
			}
			GLOBAL.processing.stroke(GLOBAL.colorButtonActive);
			GLOBAL.processing.fill(GLOBAL.colorButtonActive,5);
			GLOBAL.processing.rect(GLOBAL.processing.mouseX - dx, GLOBAL.processing.mouseY - dy, width, height);
			prevPaintX = GLOBAL.processing.mouseX;
			prevPaintY = GLOBAL.processing.mouseY;
			
			if(label != null)
			{
//				PApplet.println(label);
				//GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);
				//GLOBAL.processing.strokeWeight(1);
				GLOBAL.processing.fill(GLOBAL.colorButtonLabel);
				GLOBAL.processing.textAlign(PApplet.CENTER, PApplet.CENTER);
				GLOBAL.processing.textFont(GLOBAL.tFont,fontSize);
				if(icon!=null)
					GLOBAL.processing.text(label, GLOBAL.processing.mouseX - dx + (width/2) + 10, GLOBAL.processing.mouseY - dy + (height/2));
				else
					GLOBAL.processing.text(label, GLOBAL.processing.mouseX - dx + (width/2), GLOBAL.processing.mouseY - dy + (height/2));
			}

			if(icon != null)
			{
				GLOBAL.processing.image(icon, x + 5, x + 5, 18,18);
			}
		}
		
		
	}

